package org.finegold.core.discern.enums;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.finegold.core.discern.props.DiscernProperties;

import java.util.HashMap;
import java.util.Map;

public class BuildParams {
		private MethodType methodType;
		private IdType idType;
		private CustomType customType;
		private Map<String, String> headers;
		private JSONObject jsonObject;
		private DiscernProperties discernProperties;

		public BuildParams(MethodType methodType, IdType idType, CustomType customType,DiscernProperties discernProperties) {
			this.methodType = methodType;
			this.idType = idType;
			this.customType = customType;
			this.discernProperties = discernProperties;
		}

		public Map<String, String> getHeaders() {
			return headers;
		}

		public JSONObject getJsonObject() {
			return jsonObject;
		}

		public BuildParams invoke() throws JSONException {
			headers = new HashMap<String, String>();
			headers.put("Authorization", "APPCODE " + discernProperties.getCode());
			//根据API的要求，定义相对应的Content-Type
			headers.put("Content-Type", "application/json; charset=UTF-8");
			HashMap<String, Object> map = Maps.newHashMap();
			jsonObject = new JSONObject();
			if(MethodType.idCart.getType().equals(methodType.getType())){
				map.put("side", idType.getType());
				String config_str = new JSONObject(map).toString();
				if(map.size() > 0) {
					jsonObject.put("configure", config_str);
				}
			}else if(MethodType.custom.getType().equals(methodType.getType())){
				map.put("template_id",customType.getCode() );
				String config_str = new JSONObject(map).toString();
				if(map.size() > 0) {
					jsonObject.put("configure", config_str);
				}
			}else if(MethodType.businessLicense.getType().equals(methodType.getType())){

			}
			return this;
		}
	}
